package org.zhaijisong.Controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.zhaijisong.dao.*;
import org.zhaijisong.pojo.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import Publics.*;

@RequestMapping("/staff")
@Controller
@CrossOrigin("*")
public class StaffController {
    @Autowired
    private StaffDao staffDao;
    @Autowired
    private TransferDao transferDao;
    @Autowired
    private WorksheetDao worksheetDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    @RequestMapping("/login")
    @ResponseBody
    public ResponseResUlt show(String user,String password){
        Map<String,Object> map=new HashMap<>();
        Staff s = staffDao.login(user);
        if(s==null){
            return new ResponseResUlt(1,"没有此账号");
        }else{
            String ps=password;
            //ps=new Md5Hash(password,s.getSalt()).toString();
            if(!ps.equals(s.getPassword())){
                return new ResponseResUlt(1,"密码错误");
            }
        }
        List<Role> roles=roleDao.getByRoleId(s.getId());
        List<Permission> perms=permissionDao.getUidByPerm(s.getId());
        map.put("staff_roles",roles);
        map.put("staff_login",s);
        map.put("staff_perm",perms);
        ResponseResUlt result=new ResponseResUlt(map);
        return result;
    }
    @RequestMapping("/dingdan")
    @ResponseBody
    public ResponseResUlt dingdan(@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "10") int limit,Transfer transfer,String statestr){
        if(transfer==null){
            transfer=new Transfer();
        }
        if(StringUtils.isNotEmpty(statestr)){
            transfer.setStates(statestr.split(","));
        }
        Page<Transfer> pag= PageHelper.startPage(page,limit);
        List<Transfer> list=transferDao.getList(transfer);
        return new ResponseResUlt(pag.getTotal(),list);
    }
    @RequestMapping("/dingdan/{id}")
    @ResponseBody
    public ResponseResUlt dingdan(@PathVariable Integer id){

        return ResponseResUlt.createSuccessResUle();
    }

    @RequestMapping("/wancheng")
    @ResponseBody
    public ResponseResUlt wancheng(Transfer transfer,Worksheet worksheet,Integer tid,Integer wid){
        worksheet.setPayment(worksheet.getPayment()==null?0:worksheet.getPayment());
        transfer.setState(transfer.getState()==null?1:2);
        transfer.setId(tid);
        worksheet.setId(wid);
        transferDao.update(transfer);
        worksheetDao.update(worksheet);
        return ResponseResUlt.createSuccessResUle();
    }
    @RequestMapping("/map")
    @ResponseBody
    public ResponseResUlt getMap(Integer tid,Integer wid){
        Transfer transfer=transferDao.getByWid(tid,wid);
        Map<String,Object> map=new HashMap<>();
        String kaishi=AddresMap.getdizhi(transfer.getWorksheet().getUserAdd(),0);
        String jieshu=AddresMap.getdizhi(transfer.getWorksheet().getConsignee(),0);
        map.put("kaishi",kaishi);
        map.put("jieshu",jieshu);
        ResponseResUlt resp=new ResponseResUlt(map);
        return resp;
    }
}
